package com.dzbh.cdsdriver.push.receiver;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.baidu.android.pushservice.PushMessageReceiver;
import com.dzbh.cdsdriver.location.LocationService;
import com.dzbh.cdsdriver.location.listener.MyLocationListener;
import com.dzbh.cdsdriver.service.MyLocationService;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class PushReceiver extends PushMessageReceiver {
    private static final String TAG = "PushReceiver";
    private LocationService locationService;
    private MyLocationListener myLocationListener;
    /**
     * 调用 PushManager.startWork 后，sdk 将对 push server 发起绑定请求，这个过程是异步的。
     绑定请求的结果通过 onBind 返回。 */
    @Override
    public void onBind(Context context, int errorCode, String appid,
                       String userId, String channelId, String requestId) {
        String responseString = "onBind errorCode=" + errorCode + " appid="
            + appid + " userId=" + userId + " channelId=" + channelId + " requestId=" + requestId;
        Log.d(TAG, responseString);

        if(null == locationService){
            locationService = new LocationService(context);
            myLocationListener = new MyLocationListener();
            myLocationListener.setmContext(context);
            locationService.registerListener(myLocationListener);
            locationService.start();
        }

    }


    /**
     *  PushManager.stopWork() 的回调函数。
     * @param context
     * @param errorCode
     * @param requestId
     */
    @Override
    public void onUnbind(Context context, int errorCode, String requestId) {
        String responseString = "onUnbind errorCode=" + errorCode + " requestId = " + requestId;
        Log.d(TAG, responseString);
        if(locationService != null){
            locationService.unregisterListener(myLocationListener);
            locationService.stop();
        }
    }

    /**
     * setTags() 的回调函数。
     * @param context
     * @param errorCode
     * @param sucessTags
     * @param failTags
     * @param requestId
     */
    @Override
    public void onSetTags(Context context, int errorCode, List<String> sucessTags, List<String> failTags,
                          String requestId) {
        String responseString = "onSetTags errorCode=" + errorCode + " sucessTags="
                + sucessTags + " failTags=" + failTags + " requestId=" + requestId;
        Log.d(TAG, responseString);
    }

    /**
     * delTags() 的回调函数。
     * @param context
     * @param errorCode
     * @param sucessTags
     * @param failTags
     * @param requestId
     */
    @Override
    public void onDelTags(Context context, int errorCode, List<String> sucessTags, List<String> failTags,
                          String requestId) {
        String responseString = "onDelTags errorCode=" + errorCode + " sucessTags="
                + sucessTags + " failTags=" + failTags + " requestId=" + requestId;
        Log.d(TAG, responseString);
    }

    /**
     * listTags() 的回调函数
     * @param context
     * @param errorCode
     * @param tags
     * @param requestId
     */
    @Override
    public void onListTags(Context context, int errorCode, List<String> tags, String requestId) {
        String responseString = "onListTags errorCode=" + errorCode + " tags=" + tags;
        Log.d(TAG, responseString);
    }

    /**
     * 接收透传消息的函数。 */
    @Override
    public void onMessage(Context context, String message, String customContentString) {
        String messageString = "透传消息 message=" + message + " customContentString=" + customContentString;
        Log.d(TAG, messageString);
        /*if(null == locationService){
            locationService = new LocationService(context);
            myLocationListener = new MyLocationListener();
            myLocationListener.setmContext(context);
            locationService.registerListener(myLocationListener);
            locationService.start();
        }

        locationService.doLocation();*/
        Intent intent = new Intent(context, MyLocationService.class);
        context.startService(intent);

        // 自定义内容获取方式，mykey 和 myvalue 对应透传消息推送时自定义内容中设置的键和值
        if (customContentString != null & customContentString != "") {
            JSONObject customJson = null;
            try {
                customJson = new JSONObject(customContentString);
                String myvalue = null;
                if (!customJson.isNull("mykey")) {
                    myvalue = customJson.getString("mykey"); }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace(); }
        }
    }

    /**
     * 接收通知点击的函数。
     * */
    @Override
    public void onNotificationClicked(Context context, String title, String description,
                                      String customContentString) {
        String notifyString = "通知点击 title=" + title + " description="
                + description + " customContent=" + customContentString;
        Log.d(TAG, notifyString);
        if (customContentString != null & customContentString != "") { JSONObject customJson = null;
            try {
                customJson = new JSONObject(customContentString); String myvalue = null;
                if (!customJson.isNull("mykey")) {
                    myvalue = customJson.getString("mykey"); }
            } catch (JSONException e) {
                // TODO Auto-generated catch block e.printStackTrace();
            } }
    }

    /**
     * 接收通知到达的函数。
     * @param  context 上下文
     * @param title 推送的通知的标题
     * @param description 推送的通知的描述
     * @param customContentString 自定义内容，为空或者 json 字符串
     */
    @Override
    public void onNotificationArrived(Context context, String title, String description, String customContentString) {
        String notifyString = "通知到达 title=" + title + " description="
                + description + " customContent=" + customContentString;
        Log.d(TAG, notifyString);

        if (customContentString != null & customContentString != "") { JSONObject customJson = null;
            try {
                customJson = new JSONObject(customContentString); String myvalue = null;
                if (!customJson.isNull("mykey")) {
                    myvalue = customJson.getString("mykey"); }
            } catch (JSONException e) {
                // TODO Auto-generated catch block e.printStackTrace();
            } }
    }
}
